package com.cyou.nad.qb.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.cyou.nad.utils.jmx.MyCacheUtils;
import com.cyou.qb.common.domain.Active;
import com.cyou.qb.common.domain.BlackList;
import com.cyou.qb.common.service.ActivityService;
import com.cyou.qb.common.service.BlackListService;
import com.cyou.qb.common.service.UserDrawTraceService;
import com.cyou.qb.constants.CacheKeyConstants;

/**
 * <p>mysql与cache同步</p>
 * @author zoudongchao
 *
 */
@Service("mysqlCacheSync")
public class MysqlCacheSync {
	
	@Resource(name="blackListServiceImpl")
	private BlackListService blackListService;
	
	@Resource(name="ActivityServiceImpl")
	private ActivityService activityService;
	
	@Resource(name="UserDrawTraceServiceCacheImpl")
	private UserDrawTraceService userDrawTraceService;
	
	private Logger log = Logger.getLogger(MysqlCacheSync.class);
	/**
	 * 每日清理活动基本信息
	 */
	public void clearActivityCache() {
		log.info("start to clear activity cache...");
		this.clearCacheByNameSpace(CacheKeyConstants.DOMAIN_ACTIVE);
		log.info("clear activity cache end");
	}
	
	/**
	 * 清除每个用户每个活动的抽奖次数
	 */
	public void clearUserActiveDrawTimes(){
		log.info("start to clear user activity draw times...");
//		this.clearCacheByNameSpace(CacheKeyConstants.USER_DRAW_TRACE__USER);
		this.clearCacheByNameSpace(CacheKeyConstants.DOMAIN_USER_DRAW_TRACE_USER);
		log.info("clear user activity draw times end");
	}
	
	/**
	 * 清除指定用户中奖记录
	 */
	public void clearAssingedWinnerWinInfo(){
		log.info("start to clear assigned winner win info...");
		this.clearCacheByNameSpace(CacheKeyConstants.DOMAIN_ASSIGNED_WINNER);
		log.info("clear assigned winner win end");
	}
	
	/**
	 * 删除三天前的数据，供定时任务调用
	 */
	public void clearWinnerTop(){
		log.info("start to clear winner top 3 days before...");
		this.clearWinnerTop(3);
		log.info("clear winner top 3 days before end");
	}
	
	/**
	 * <p>同步活动抽奖次数到db</p>
	 * 插入前判断值是否变化
	 */
	public void syncDrawTimes(){
		log.info("start to sync activity draw times...");
		//获取目前总活动数
		List<Active> activeList = activityService.findAllActive();
		for (Active active : activeList) {
			Long drawTimes = userDrawTraceService.findActiveDrawTimes(active.getId());
			if (drawTimes == active.getDrawNum())
				continue;//相同则跳过，进行下一条记录比较
			activityService.updateActiveDrawNum(active.getId().intValue(), drawTimes);
		}
		log.info("sync activity draw times end");
	}
	
	/**
	 * <p>每日读库更新黑名单Cache数据</p>
	 * @return
	 */
	public void syncBlackListCache(){
		log.info("start to update blacklist cache...");
		Map<String,List<BlackList>> map =  new HashMap<String, List<BlackList>>();
		//查找所有有效黑名单
		List<BlackList> list= blackListService.findAllValidBlackList();
		if(list==null||list.isEmpty())//无
			return;
		
		for (BlackList blackList : list) {
			List<BlackList> tmp = map.get(blackList.getPassport());//有passport查找出黑名单记录
			if(tmp==null){
				tmp=new ArrayList<BlackList>();
			}
			tmp.add(blackList);
			map.put(blackList.getPassport(), tmp);//更新
		}
		MyCacheUtils.put(CacheKeyConstants.DOMAIN_BLACK_LIST, "LIST", map);
		log.info("update blacklist cache end");
	}
	
	/**
	 * 清除所有活动的前N个的中奖名单
	 */
	private void clearWinnerTop(int days){
		//查找已结束3天的活动
		List<Active> activeList = activityService.findEndedActivity(days);
		for (Active active:activeList) {
			MyCacheUtils.delete(CacheKeyConstants.DOMAIN_TOP_WINNER, "WINNER_"+active.getId());
		}
	}


	private void clearCacheByNameSpace(String nameSpace) {
		MyCacheUtils.clearByNameSpace(nameSpace);
	}
}
